<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - logger_custom_output_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font>
<font color='#009900'>/*

    This is an example showing how to control where the dlib::logger sends its messages.
    This is done by creating a "hook" class that is called whenever any of the loggers want
    to log a message.  The hook class then outputs the messages using any method you like.


    Prior to reading this example, you should understand the basics of the dlib::logger.
    So you should have already read the <a href="logger_ex.cpp.html">logger_ex.cpp</a> and <a href="logger_ex_2.cpp.html">logger_ex_2.cpp</a> example programs.

*/</font>


<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>logger.h<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;

<font color='#0000FF'>class</font> <b><a name='my_hook'></a>my_hook</b>
<b>{</b>
<font color='#0000FF'>public</font>:
    <b><a name='my_hook'></a>my_hook</b><font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> 
    <b>{</b>
        fout.<font color='#BB00BB'>open</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>my_log_file.txt</font>"<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='log'></a>log</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> string<font color='#5555FF'>&amp;</font> logger_name,
        <font color='#0000FF'>const</font> log_level<font color='#5555FF'>&amp;</font> ll,
        <font color='#0000FF'>const</font> uint64 thread_id,
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> message_to_log
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>// Log all messages from any logger to our log file.
</font>        fout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> ll <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> [</font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>thread_id<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>"<font color='#CC0000'>] </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> logger_name <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> message_to_log <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

        <font color='#009900'>// But only log messages that are of LINFO priority or higher to the console.
</font>        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>ll <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> LINFO<font face='Lucida Console'>)</font>
            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> ll <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> [</font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>thread_id<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>"<font color='#CC0000'>] </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> logger_name <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> message_to_log <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    <b>}</b>

<font color='#0000FF'>private</font>:
    ofstream fout;
<b>}</b>;

<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
    my_hook hook;
    <font color='#009900'>// This tells all dlib loggers to send their logging events to the hook object.  That
</font>    <font color='#009900'>// is, any time a logger generates a message it will call hook.log() with the message
</font>    <font color='#009900'>// contents.  Additionally, hook.log() will also only be called from one thread at a
</font>    <font color='#009900'>// time so it is safe to use this kind of hook in a multi-threaded program with many
</font>    <font color='#009900'>// loggers in many threads.
</font>    <font color='#BB00BB'>set_all_logging_output_hooks</font><font face='Lucida Console'>(</font>hook<font face='Lucida Console'>)</font>;
    <font color='#009900'>// It should also be noted that the hook object must not be destructed while the
</font>    <font color='#009900'>// loggers are still in use.  So it is a good idea to declare the hook object 
</font>    <font color='#009900'>// somewhere where it will live the entire lifetime of the program, as we do here.
</font>

    logger <font color='#BB00BB'>dlog</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>main</font>"<font face='Lucida Console'>)</font>;
    <font color='#009900'>// Tell the dlog logger to emit a message for all logging events rather than its
</font>    <font color='#009900'>// default behavior of only logging LERROR or above. 
</font>    dlog.<font color='#BB00BB'>set_level</font><font face='Lucida Console'>(</font>LALL<font face='Lucida Console'>)</font>;

    <font color='#009900'>// All these message go to my_log_file.txt, but only the last two go to the console.
</font>    dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LDEBUG <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a debugging message.</font>";
    dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LINFO  <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is an informational message.</font>";
    dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LERROR <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>An error message!</font>";
<b>}</b>


</pre></body></html>